package com.erp.module.jingXiHua

import android.content.Context
import android.util.Log
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.FileBean
import com.erp.bean.JingXiHuaBean
import com.erp.bean.JingXiHuaSouSuoBean
import com.erp.bean.PhotoBean
import com.erp.mclass.UnCeHandler
import com.erp.module.zhiLiang.xingJianZhiLiang.IXJWuLiaoZhiLiangKp
import com.erp.pull.BasePullP
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.util.*

/**
 * Created by Administrator on 2019/4/13/013.
 * 作者：刘洋
 */
class IJingXiHuaP(var V : JingXiHuaContract.JingXiHuaView,var context: Context,var list : MutableList<JingXiHuaBean>): BasePullP(),JingXiHuaContract.JingXiHuaP {

    var md = JingXiHuaModel()
    var sousuo = JingXiHuaSouSuoBean()
    init {
        PullV=V
    }
    override fun getData(page: Int, pagesize: Int, onUserDataInterface: OnUserDataInterface) {
        md.JingXiHua(context,page,pagesize,sousuo,onUserDataInterface)
    }

    override fun jsondata(data: String?) {
        if(data==null){
            return
        }
        try {
            var js = JSONArray(data)
            for (i in 0 until js.length()){
                var jo = js.get(i) as JSONObject
                var bean = JingXiHuaBean()
                bean.id=JsonUtil.getvolue(jo,"id")
                bean.content_scheme=JsonUtil.getvolue(jo,"content_scheme")
                bean.file_category=JsonUtil.getvolue(jo,"file_category")
                bean.ctime=JsonUtil.getvolue(jo,"ctime")
                bean.duty_department=JsonUtil.getvolue(jo,"duty_department")
                bean.duty_name=JsonUtil.getvolue(jo,"duty_name")
                bean.guidance_name=JsonUtil.getvolue(jo,"guidance_name")
                bean.audit_name=JsonUtil.getvolue(jo,"audit_name")
                bean.audit_time=JsonUtil.getvolue(jo,"audit_time")
                bean.approval_name=JsonUtil.getvolue(jo,"approval_name")
                bean.approval_time=JsonUtil.getvolue(jo,"approval_time")
                bean.approval_status=JsonUtil.getvolue(jo,"approval_status")
                bean.audit_status=JsonUtil.getvolue(jo,"audit_status")
                bean.creator_uid=JsonUtil.getvolue(jo,"creator_uid")
                bean.creator_uid=JsonUtil.getvolue(jo,"creator_uid")
                bean.audit_defeated_reason=JsonUtil.getvolue(jo,"audit_defeated_reason")
                bean.approval_defeated_reason=JsonUtil.getvolue(jo,"approval_defeated_reason")
                bean.audit_gonghao=JsonUtil.getvolue(jo,"audit_gonghao")
                bean.approval_gonghao=JsonUtil.getvolue(jo,"approval_gonghao")
                var state = ""
                if(bean.audit_status!="1"){
                    if(bean.audit_status=="0"){
                        state = "未审核";
                    }else if(bean.audit_status=="2"){
                        state = "审核未通过"
                    }
                }else{
                    if(bean.approval_status=="0"){
                        state = "已审核未审批"
                    }else if(bean.approval_status=="2"){
                        state = "审批未通过"
                    }else if(bean.approval_status== "1"){
                        state = "已审批"
                    }
                }
                var jsf = JsonUtil.getjsvolue(jo,"files")
                for (j in 0 until jsf.length()) {
                    var jof = jsf.get(j) as JSONObject
                    var filemime = JsonUtil.getvolue(jof, "filemime")
                    if(filemime=="image/jpeg"){
                        var photo = PhotoBean()
                        photo.fid=JsonUtil.getvolue(jof,"fid")
                        photo.filename=JsonUtil.getvolue(jof,"filename")
                        photo.uri=JsonUtil.getvolue(jof,"uri")
                        bean.listphoto.add(photo)
                    }else{
                        var file = FileBean()
                        file.fid=JsonUtil.getvolue(jof,"fid")
                        file.filename=JsonUtil.getvolue(jof,"filename")
                        file.uri=JsonUtil.getvolue(jof,"uri")
                        file.filemime=filemime
                        bean.listfile.add(file)
                    }
                }
                bean.state=state
                list.add(bean)
            }
        }catch (e : JSONException){
            e.printStackTrace()
        }
        Log.d("PhotoAdapter",list.toString())
    }

    override fun listClean() {
        list.clear()
    }

    override fun sousuo(sousuo: JingXiHuaSouSuoBean) {
        this.sousuo=sousuo
        xiala()
    }

    override fun getGongHaoLieBiao() {
        UnCeHandler.buzhou="getGongHaoLieBiao"
        if(IXJWuLiaoZhiLiangKp.gonghaos !=""){
            getgonghaojson(IXJWuLiaoZhiLiangKp.gonghaos)
            return
        }
        md.getZhiLiangGongHaoLieBiao(context, object : OnUserDataInterface {
            override fun onUserData(data: String) {
                Myutil.MyTost("获取工号列表")
                IXJWuLiaoZhiLiangKp.gonghaos = data
                getgonghaojson(data)
            }

            override fun onNot(throwable: String?) {
                Log.d("TAAAG", "throwable=" + Myutil.decodeUnicode(throwable))
                if (throwable != null) {
                    V.getGongHaoLieBiaoNo(throwable)
                }
            }

        })
    }
    fun getgonghaojson(data: String){
        UnCeHandler.buzhou="getgonghaojson"
        var gonghao: MutableList<String> = ArrayList()
        var gonghaoname: MutableList<String> = ArrayList()
        try {
            val jo = JSONObject(data)
            val keys = jo.keys()
            while (keys.hasNext()) {
                val key = keys.next() as String
                gonghao.add(key)
                gonghaoname.add(JsonUtil.getvolue(jo, key))
            }
            gonghao.removeAt(0)
            gonghaoname.removeAt(0)
            V.getGongHaoLieBiaoOk(gonghao, gonghaoname)
        } catch (e: Exception) {
            Log.d("TAAAG", "e=" + e.toString())
            e.printStackTrace()
        }
    }
}